package com.ruoyi.web.cp.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.web.core.config.ConfigLoader;
import com.ruoyi.web.rmNew.service.http.AqHttpService2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AqService {
    @Autowired
    private AqHttpService2 aqHttpService2;
    @Autowired
    private ConfigLoader configLoader;

    public List<String> getList() {
        String[] filterTypes = {"today", "tomorrow"};
        JSONArray records = new JSONArray();
        for (String type : filterTypes) {
            int page = 1;
            JSONObject jsonObject = aqHttpService2.getAqList(page, type);
            JSONObject data = jsonObject.getJSONObject("data");
            Integer total = data.getJSONObject("pagination").getInteger("total");
            Integer size = data.getJSONObject("pagination").getInteger("size");
            records.addAll(data.getJSONArray("list"));
            for (int i = 0; i < total / size; i++) {
                page++;
                JSONObject json = aqHttpService2.getAqList(page, type);
                records.addAll(json.getJSONObject("data").getJSONArray("list"));
            }
        }
        return parseResult(records);
    }

    private List<String> parseResult(JSONArray records) {
        List<String> list = new ArrayList<>();
        String text = "{}\t{}\t{}\t{}\t{}";
        for (int i = 0; i < records.size(); i++) {
            try {
                JSONObject record = records.getJSONObject(i);
                String id = record.getString("id");
                String name1 = record.getString("homeTeam");
                String name2 = record.getString("awayTeam");
                String lg = record.getString("leagueName");
                String date = record.getString("matchTime");
                DateTime dateTime = DateUtil.parse(date);
                // 获取第二天中午12点的时间
                DateTime nextDayNoon = DateUtil.beginOfDay(new Date())
                        .offset(DateField.DAY_OF_YEAR, 1)
                        .offset(DateField.HOUR, 12);

                // 判断当前时间是否大于第二天中午12点
                if (dateTime.isAfter(nextDayNoon)) {
                    continue;
                }

                String str = StrUtil.format(text, id, lg, date, name1, name2).replace("\n", "");
                list.add(str);
            } catch (Exception e) {
                log.error("解析异常：{}", e.getMessage());
            }
        }
        FileUtil.writeLines(list, configLoader.getRmPath() + "aq-game.txt", "utf8");
        return list;
    }

    public void getDetail() {
        List<String> games = FileUtil.readLines(configLoader.getRmPath() + "aq-game.txt", "utf8");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < games.size(); i++) {
            try {
                list.add(games.get(i));
                String[] strs = games.get(i).split("\t");
                String path = StrUtil.format("{}aq-gamescore\\\\\\{}.txt",
                        configLoader.getRmPath(), strs[0]);
                String content = FileUtil.readString(path, "utf8");
                JSONObject data = JSONObject.parseObject(content);
                if (!"1000".equals(data.getString("code"))) {
                    continue;

                }
                JSONArray items = data.getJSONArray("data");
                for (int j = 0; j < items.size(); j++) {
                    JSONObject item = items.getJSONObject(j);
                    list.add(StrUtil.format("{}\t{}",
                            item.getString("label").replace(":", "-"),
                            getPlv(item.getString("value"))));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        FileUtil.writeLines(list, configLoader.getRmPath() + "aq-gameScore.txt", "utf8");
    }

    private String getPlv(String plv) {
        BigDecimal bigDecimal = new BigDecimal(plv);
        return bigDecimal.subtract(new BigDecimal("1")).multiply(new BigDecimal("100")).toString();
    }
}
